﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using ThousandFunClient.model;
using log4net;
using System.Drawing;

using System.Windows.Forms;

namespace ThousandFunClient.model
{
    public class TFModel
    {
        public TFModel(string strSendinit,string strReplyinit)
        {
            this.strSend = strSendinit;
            this.strReply = strReplyinit;
        }

        public TFModel()
        {
        }


        public string StrSend
        {
            get {
                if (strSend == null)
                {
                    initSendDate();
                }
                return strSend;
            }
            set { strSend = value; }
        }

        public string StrReply
        {
            get { return strReply; }
            set { strReply = value; }
        }

        public string Startbyte
        {
            get { return startbyte; }
            set { startbyte = value; }
        }

        public string LOProtocol
        {
            get { return lOProtocol; }
            set { lOProtocol = value; }
        }
        
          public string DOCommand
        {
            get { return dOCommand; }
            set { dOCommand = value; }
        }


          public string SOCommand
        {
            get { return sOCommand; }
            set { sOCommand = value; }
        }

         public string Separator
        {
            get { return separator; }
            set { separator = value; }
        }
    
         public string Oindex
        {
            get { return oindex; }
            set { oindex = value; }
        }

         public string Osubindex
        {
            get { return osubindex; }
            set { osubindex = value; }
        }

                 public string Checksum
        {
            get { return checksum; }
            set { checksum = value; }
        }


                  public string DataType
        {
            get { return dataType; }
            set { dataType = value; }
        }

            public string Answerdata
        {
            get { return answerdata; }
            set { answerdata = value; }
        }

            public string AnswerdataLog
            {
                get { return answerdatalog; }
                set { answerdatalog = value; }
            }

        private string strSend = "";
        private string startbyte = "";
        private string lOProtocol = "";
        private string dOCommand = "";
        private string separator = "";
        private string oindex= "";
        private string osubindex = "";
        private string checksum = "";

         private string sOCommand = "";
         private string strReply = "";
           private string   dataType = "";
           private string answerdata = "";
           private string answerdatalog = "";

           //private int strSend = "";
           private int startbyteint ;
           private int lOProtocolint ;
           private int dOCommandint ;
           private int separatorint ;
           private int oindexint ;
           private int osubindexint ;
           private int checksumint ;

           private int sOCommandint ;
           //private string strReply = "";
           private int dataTypeint ;
           private int answerdataint ;


           public string ToLogString()
           {
               string datatrans = "";
               int l = answerdatalog.Length;
               for (int i = 2; i <= l; i = i + 2)
               {
                   datatrans = datatrans + answerdatalog.Substring(i - 2, 2) + " ";
               }
               return startbyte + " " + lOProtocol + " " + dOCommand + " " + sOCommand + " " + separator + " " + dataType + " " + datatrans  + checksum;
           }


           public int Startbyteint
           {
               get { return startbyteint; }
               set { startbyteint = value; }
           }

           public int LOProtocolint
           {
               get { return lOProtocolint; }
               set { lOProtocolint = value; }
           }

           public int DOCommandint
           {
               get { return dOCommandint; }
               set { dOCommandint = value; }
           }


           public int SOCommandint
           {
               get { return sOCommandint; }
               set { sOCommandint = value; }
           }

           public int Separatorint
           {
               get { return separatorint; }
               set { separatorint = value; }
           }

           public int Oindexint
           {
               get { return oindexint; }
               set { oindexint = value; }
           }

           public int Osubindexint
           {
               get { return osubindexint; }
               set { osubindexint = value; }
           }

           public int Checksumint
           {
               get { return checksumint; }
               set { checksumint = value; }
           }


           public int DataTypeint
           {
               get { return dataTypeint; }
               set { dataTypeint = value; }
           }

           public int Answerdataint
           {
               get { return answerdataint; }
               set { answerdataint = value; }
           }


        public void initSendDate()
        {
            Startbyte = "00";
            Separator = "0B";
            computeLength();
            strSend =startbyte + lOProtocol + dOCommand+separator+oindex+osubindex+checksum;
        }


        public void initReplyDate()
        {
            Startbyte = "00";
            Separator = "8B";
        }

        public void computeLength()
        {
            int intlen = DOCommand.Length + SOCommand.Length + Separator.Length + Oindex.Length + Osubindex.Length + DataType.Length + Answerdata.Length;
            LOProtocol = intlen.ToString();
        }
    }

    public class TFConfig
    {
         private static TFConfig instance;

         private TFConfig()
        {
        
        }

         public static TFConfig GetInstance()
        {
                if(instance==null)
                {
                    instance = new TFConfig();
                    instance.init();
                }
                return instance;
        }

        public void init()
        {

            htRunning = new Hashtable();
            htBatchCMD = new Hashtable(); //创建一个Hashtable实例
            htTimer = new Hashtable();
            htLogger = new Hashtable();
            //排队发指令
            htRunning.Add(0, Constants.Cycle);
            htRunning.Add(1, Constants.Torque);
            htRunning.Add(2, Constants.Angle);
            htRunning.Add(3, Constants.ProgramNo);
            htRunning.Add(4, Constants.ProgramStep);
            htRunning.Add(5, Constants.PMStatus);

            XElement xe = XElement.Load(ConfigXMLAddress);
            XElement ePortA = xe.Element("PortA");

            portConfigA = new PortConfig(ePortA);
            XElement ePortB = xe.Element("PortB");
            portConfigB = new PortConfig(ePortB);


            XElement ePortC = xe.Element("PortC");
            portConfigC = new PortConfig(ePortC);

            XElement eTorque = xe.Element("TorqueConfig");

            torqueConfig = new SendCMDConfig(eTorque);

            XElement eAngle = xe.Element("AngleConfig");

            angleConfig = new SendCMDConfig(eAngle);

            
            XElement eCycleNumberConfig = xe.Element("CycleNumberConfig");

            cycleNumberConfig = new SendCMDConfig(eCycleNumberConfig);


            XElement eBatchCMD = xe.Element("BatchCMD");

            
          

           
            
            IEnumerable<XNode> ebatchNodes = eBatchCMD.Nodes();
            //ebatchNodes.Count();
            foreach (XNode xNode in ebatchNodes)
            {
                XElement cc = (XElement)xNode;
                SendCMDConfig sendCMDConfig = new SendCMDConfig(cc);


                htBatchCMD.Add(sendCMDConfig.SendCMD, sendCMDConfig);

                if (!htLogger.ContainsKey(sendCMDConfig.LoggerName))
                {
                    htLogger.Add(sendCMDConfig.LoggerName, LogManager.GetLogger(sendCMDConfig.LoggerName));
                }
                if (!htTimer.ContainsKey(sendCMDConfig.Interval))
                {
                    htTimer.Add(sendCMDConfig.Interval,new System.Timers.Timer(Double.Parse(sendCMDConfig.Interval)));
                }

            }


            //batchCMD = new List<string>[eBatchCMDs.Length];
        }




        public PortConfig PortConfigA
        {
            get { return portConfigA; }
            set { portConfigA = value; }
        }

        public PortConfig PortConfigB
        {
            get { return portConfigB; }
            set { portConfigB = value; }
        }
        public PortConfig PortConfigC
        {
            get { return portConfigC; }
            set { portConfigC = value; }
        }

        public List<String> BatchCMD
        {
            get { return batchCMD; }
            set { batchCMD = value; }
        }

        public Hashtable HtBatchCMD
        {
            get { return htBatchCMD; }
            set { htBatchCMD = value; }
        }

        public Hashtable HtTimer
        {
            get { return htTimer; }
            set { htTimer = value; }
        }

        public Hashtable HtLogger
        {
            get { return htLogger; }
            set { htLogger = value; }
        }

        public SendCMDConfig TorqueConfig
        {
            get { return torqueConfig; }
            set { torqueConfig = value; }
        }

        public SendCMDConfig AngleConfig
        {
            get { return angleConfig; }
            set { angleConfig = value; }
        }

        public SendCMDConfig CycleNumberConfig
        {
            get { return cycleNumberConfig; }
            set { cycleNumberConfig = value; }
        }


        public Hashtable HtRunning
        {
            get { return htRunning; }
            set { htRunning = value; }
        }


        private Hashtable htRunning;

        private Hashtable htBatchCMD; //创建一个Hashtable实例
        private Hashtable htTimer;
        private Hashtable htLogger;

        //private Hashtable htRunning = new Hashtable();

        //private Hashtable htBatchCMD = new Hashtable(); //创建一个Hashtable实例
        //private Hashtable htTimer = new Hashtable();
        //private Hashtable htLogger = new Hashtable();

        private SendCMDConfig cycleNumberConfig;
        private SendCMDConfig angleConfig;
        private SendCMDConfig torqueConfig;
        private List<String> batchCMD ; 
        private PortConfig portConfigA ;

        private PortConfig portConfigB;

        private PortConfig portConfigC;

        public static string ConfigXmlName = "ThousandFunConfig.xml";
        public static string ConfigXMLAddress = System.AppDomain.CurrentDomain.BaseDirectory + ConfigXmlName;
    }

    public class ToolsNetResponse 
    {
        public ToolsNetResponse()
        {
        }
        public ToolsNetResponse(string sResponse)
        {
            resultCMD = sResponse.Substring(4, 2);

            sResponseLength = sResponse.Substring(0, 4);

            sResponseReal = sResponse.Substring(4, sResponse.Length - 4);

            if (resultCMD.Equals("12"))
            {
                identifier12 = sResponseReal.Substring(2, 5);
            }

            if (resultCMD.Equals("08"))
            {
                identifier08 = sResponseReal.Substring(2, 5);
                resultSequencenumber08 = sResponseReal.Substring(7, 5);
                eventSequenceNumber08 = sResponseReal.Substring(12, 5);
                tnIPaddress08 = sResponseReal.Substring(17, 15);
                tnPortNumber08 = sResponseReal.Substring(32, 5);
            }
            if (resultCMD.Equals("05"))
            {
                identifier05 = sResponseReal.Substring(2, 5);
                timeStamp05 = sResponseReal.Substring(7, 14);
                errorCode05 = sResponseReal.Substring(21, 3);
            }
        }
        
        public string Identifier12
        {
            get { return identifier12; }
            set { identifier12 = value; }
        }
        //公用参数
        public string ResultCMD
        {
            get { return resultCMD; }
            set { resultCMD = value; }
        }

        public string SResponseLength
        {
            get { return sResponseLength; }
            set { sResponseLength = value; }
        }

        public string SResponseReal
        {
            get { return sResponseReal; }
            set { sResponseReal = value; }
        }

        public string Identifier08
        {
            get { return identifier08; }
            set { identifier08 = value; }
        }

        public string ResultSequencenumber08
        {
            get { return resultSequencenumber08; }
            set { resultSequencenumber08 = value; }
        }

        public string EventSequenceNumber08
        {
            get { return eventSequenceNumber08; }
            set { eventSequenceNumber08 = value; }
        }

        public string TnIPaddress08
        {
            get { return tnIPaddress08; }
            set { tnIPaddress08 = value; }
        }

        public string TnPortNumber08
        {
            get { return tnPortNumber08; }
            set { tnPortNumber08 = value; }
        }

        public string Identifier05
        {
            get { return identifier05; }
            set { identifier05 = value; }
        }

        public string TimeStamp05
        {
            get { return timeStamp05; }
            set { timeStamp05 = value; }
        }

        public string ErrorCode05
        {
            get { return errorCode05; }
            set { errorCode05 = value; }
        }

        private string sResponseLength = "";

        private string sResponseReal = "";

        private string resultCMD = "";

        private string identifier12 = "";

        private string identifier08 = "";

        private string resultSequencenumber08 = "";

        private string eventSequenceNumber08 = "";

        private string tnIPaddress08 = "";

        private string tnPortNumber08 = "";

        private string identifier05 = "";

        private string timeStamp05 = "";

        private string errorCode05 = "";

    }

    public class BatchData : ICloneable
    {
        public string TorqueSend
        {
            get { return torqueSend; }
            set { torqueSend = value; }
        }

        public string AngleSend
        {
            get { return angleSend; }
            set { angleSend = value; }
        }

        public string TorqueValue
        {
            get { return torqueValue; }
            set { torqueValue = value; }
        }

        public string AngleValue
        {
            get { return angleValue; }
            set { angleValue = value; }
        }

        public string ProgramNoValue
        {
            get { return programNoValue; }
            set { programNoValue = value; }
        }

        public string PMStatusValue
        {
            get { return pmstatusValue; }
            set { pmstatusValue = value; }
        }

        public string ProgramStepValue
        {
            get { return programstepValue; }
            set { programstepValue = value; }
        }

        public int CycleNum
        {
            get { return cycleNum; }
            set { cycleNum = value; }
        }


        public string toSendStr()
        {
            return "S|" + cycleNum.ToString() + "|" + torqueValue + "|" + angleValue + "|" + PMStatuscsv + "|" + System.DateTime.Now.ToString() + "|E";
        }

        public string PMStatuscsv
        {
            get {
                if (pmstatusValue.Equals("0"))
                {
                    pmstatuscsv = "OK";
                }
                else
                {
                    pmstatuscsv = "NOK";
                }
                return pmstatuscsv;
            }
            set { pmstatuscsv = value; }
        }


        public string toCSVStr()
        {


            return cycleNum.ToString() + "," + torqueValue + "," + angleValue + "," + programNoValue + "," + programstepValue + "," + PMStatuscsv;
        }

        private string programstepValue;
        private string pmstatusValue;
        private string programNoValue;
        private string torqueValue;
        private string angleValue;

        private string pmstatuscsv;

        private string torqueSend;
        private string angleSend;
        private int cycleNum;


        #region ICloneable 成员

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        #endregion
    }

 
    public static class Constants
    {
        public static string TFEncryptkey = "jqz4cdh";
        public static string error_msg = "异常错误，请联系管理员";
        public static string Torque = "Torque";
        public static string Angle = "Angle";
        public static string ProgramNo = "ProgramNo";
        public static string PMStatus = "PMStatus";
        public static string ProgramStep = "ProgramStep";

        public static string Cycle = "Cycle";

        public static string NullStr = "Null";

        public static Font TextBoxFont = new System.Drawing.Font("宋体", 45F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

        public static string MainPassword = System.Configuration.ConfigurationSettings.AppSettings["mainPassword"];

        public static readonly ILog syslogger = LogManager.GetLogger("ThousandFun.client.system");
        public static readonly ILog loggerSend = LogManager.GetLogger("ThousandFun.client.send");
        public static readonly ILog loggersp = LogManager.GetLogger("ThousandFun.client.translog");
        public static readonly ILog loggerfinal = LogManager.GetLogger("ThousandFun.client.finallog");

       public static string ProgramNoCMD ="00 06 02 01 0B 00 40 40 6B";
       public static string PMStatusCMD="00 06 02 01 0B 04 20 00 C7";
       public static string ProgramStepCMD = "00 06 02 01 0B 00 40 41 6A";

        public static void setTextBox(TextBox txtbox)
        {
            txtbox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            txtbox.Font = new System.Drawing.Font("宋体", 45F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            txtbox.ForeColor = System.Drawing.SystemColors.WindowFrame;
            txtbox.Multiline = true;
            txtbox.Size = new System.Drawing.Size(129, 106);
            txtbox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            txtbox.Enabled = false;
        }

    }

    public class XMLImportConfig
    {
        public XMLImportConfig(XmlNode node)
        {
            databasecolumn = node.Attributes["databasecolumn"].Value;
            xmlpathvalue = node.Attributes["xmlpathvalue"].Value;
            importtype = node.Attributes["importtype"].Value;
        }

        private string databasecolumn;

        private string xmlpathvalue;

        private string importtype;

        public string Databasecolumn
        {
            get { return databasecolumn; }
            set { databasecolumn = value; }
        }

        public string Xmlpathvalue
        {
            get { return xmlpathvalue; }
            set { xmlpathvalue = value; }
        }

        public string Importtype
        {
            get { return importtype; }
            set { importtype = value; }
        }

    }

    public class PortConfig
    {

        public PortConfig()
        {
        }
        public PortConfig(XElement ePortA)
        {
            XElement ePortName = ePortA.Element("PortName");
            portName = ePortName.Value;
            XElement eHandshake = ePortA.Element("Handshake");
            handshake = eHandshake.Value;

            XElement eBaudRate = ePortA.Element("BaudRate");
            baudRate = eBaudRate.Value;

            XElement eReadTimeout = ePortA.Element("ReadTimeout");
            readTimeout = eReadTimeout.Value;

            if (portName.Equals(Constants.NullStr))
            {
                BcouldUsed = false;
            }

        }

        public string Handshake
        {
            get { return handshake; }
            set { handshake = value; }
        }

        public string StopBits
        {
            get { return stopBits; }
            set { stopBits = value; }
        }

        public string DataBits
        {
            get { return dataBits; }
            set { dataBits = value; }
        }

        public string Parity
        {
            get { return parity; }
            set { parity = value; }
        }

        public string BaudRate
        {
            get { return baudRate; }
            set { baudRate = value; }
        }

        public string PortName
        {
            get { return portName; }
            set { portName = value; }
        }

        public string ReadTimeout
        {
            get { return readTimeout; }
            set { readTimeout = value; }
        }

        public bool BcouldUsed
        {
            get { return bcouldUsed; }
            set { bcouldUsed = value; }
        }

        private bool bcouldUsed = true;
        private string portName = "";
        private string baudRate = "";
        private string parity = "";
        private string dataBits = "";
        private string stopBits = "";
        private string handshake = "";
        private string readTimeout = "";
       
    }
   

}
